package cn.lyjuan.android.demofive.ui.slide.viewpage;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import cn.lyjuan.android.demofive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chad on 2016/3/21.
 */
public class ViewPageActivity extends FragmentActivity
{
    private ViewPager pager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        pager = new ViewPager(this);
        pager.setId(R.id.ui_viewpager_tabhost);

        this.setContentView(pager);

        TabAdapter adp = new TabAdapter(this, this.pager);
        ActionBar bar = this.getActionBar();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_USE_LOGO);

        adp.addTab(bar.newTab().setText("First"), FirstFragment.class, null);
        adp.addTab(bar.newTab().setText("Second"), SecondFragment.class, null);

        bar.setSelectedNavigationItem(0);
    }

    public static class TabAdapter extends FragmentPagerAdapter
        implements ViewPager.OnPageChangeListener, ActionBar.TabListener
    {
        static class TabInfo
        {
            public Class<?> cls;
            public Bundle ble;

            public TabInfo(Class<?> cls, Bundle ble) {this.cls = cls;this.ble = ble;}
        }

        public void addTab(ActionBar.Tab tab, Class<?> cls, Bundle ble)
        {
            TabInfo info = new TabInfo(cls, ble);

            tab.setTag(info);
            tab.setTabListener(this);

            // ActionBar 和 tabs 中的索引位置相同
            this.tabs.add(info);

            this.bar.addTab(tab);

            this.notifyDataSetChanged();
        }


        private FragmentActivity activity;
        private ViewPager pager;
        private ActionBar bar;
        private List<TabInfo> tabs = new ArrayList<>();

        public TabAdapter(FragmentActivity activity, ViewPager pager)
        {
            super(activity.getSupportFragmentManager());

            this.activity = activity;
            this.pager = pager;
            this.bar = this.activity.getActionBar();

            this.pager.setAdapter(this);

            this.pager.setOnPageChangeListener(this);

        }

        /* ************* FragmentPagerAdapter ***************** */
        @Override
        public Fragment getItem(int i)
        {
            TabInfo tab = this.tabs.get(i);
            return Fragment.instantiate(this.activity, tab.cls.getName(), tab.ble);
        }

        @Override
        public int getCount()
        {
            return this.tabs.size();
        }
        /* ************* FragmentPagerAdapter end ***************** */

        /* ************* OnPageChangeListener ***************** */
        @Override
        public void onPageScrolled(int i, float v, int i1)
        {

        }

        @Override
        public void onPageSelected(int position)
        {
            this.bar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int i)
        {

        }
        /* ************* OnPageChangeListener end ***************** */

        /* ************* TabListener ***************** */
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
        {
            Object tag = tab.getTag();

            for (int i = 0; i < this.tabs.size(); i++)
            {
                if (this.tabs.get(i) == tag)
                    this.pager.setCurrentItem(i);
            }
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
        {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
        {

        }
        /* ************* TabListener end ***************** */

    }
}
